package org.plr.ecommerce.view;

import org.apache.wicket.Page;
import org.apache.wicket.Response;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.request.IRequestCycleProcessor;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

public class RequestCycle extends WebRequestCycle {

    private SessionFactory sessionFactory = null;

    public RequestCycle(WebApplication webApplication, WebRequest webRequest, Response response) {
        super(webApplication, webRequest, response);
    }

    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public static RequestCycle get() {
        return (RequestCycle) org.apache.wicket.RequestCycle.get();
    }

    @Override
    protected void onBeginRequest() {
        System.out.println("onBeginRequest");
        if (null == this.sessionFactory) {
            NullPointerException nullPointerException = new NullPointerException("Session factory is null.");
            throw new RuntimeException("Error while beginning hibernate transaction", nullPointerException);
        }

        try {
            this.sessionFactory.getCurrentSession().beginTransaction();
        } catch (HibernateException hibernateException) {
            throw new RuntimeException("Error while beginning hibernate transaction", hibernateException);
        }

        super.onBeginRequest();
    }

    @Override
    protected void onEndRequest() {
        System.out.println("onEndRequest");
        if (null == this.sessionFactory) {
            NullPointerException nullPointerException = new NullPointerException("Session factory is null.");
            throw new RuntimeException("Error while commiting hibernate transaction", nullPointerException);
        }

        try {
            this.sessionFactory.getCurrentSession().flush();
            this.sessionFactory.getCurrentSession().getTransaction().commit();
        } catch (HibernateException hibernateException) {
            this.sessionFactory.getCurrentSession().getTransaction().rollback();
            throw new RuntimeException("Error while commiting hibernate transaction", hibernateException);
        }

        super.onEndRequest();
    }

    @Override
    public Page onRuntimeException(Page page, RuntimeException runtimeException) {
        System.out.println("onRuntimeException");
        if (null == this.sessionFactory) {
            NullPointerException nullPointerException = new NullPointerException("Session factory is null.");
            throw new RuntimeException("Error while rollbacking hibernate transaction", nullPointerException);
        }

        try {
            this.sessionFactory.getCurrentSession().getTransaction().rollback();
        }  catch (HibernateException hibernateException) {
            throw new RuntimeException("Error while rollbacking hibernate transaction", hibernateException);
        }

        return super.onRuntimeException(page, runtimeException);
    }
}
